package com.genexususa.eo.bing.data.requests;

import com.genexususa.eo.bing.data.GxItemException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GxKeywordDestinationUrlsRequest {
    private long gxCampaignId;
    private long bingCampaignId;
    private GxKeywordDestinationUrlsAdGroupRequest[] keywordDestinationUrlsAdGroupRequests;

    public GxKeywordDestinationUrlsRequest(Element element) throws IllegalArgumentException, GxItemException {
        if (element.getElementsByTagName("GxCampaignId").getLength() > 0 &&
            element.getElementsByTagName("GxCampaignId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("GxCampaignId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.gxCampaignId = Long.valueOf(((Node)element.getElementsByTagName("GxCampaignId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing external Campaign id");
        }
        if (element.getElementsByTagName("BingCampaignId").getLength() > 0 &&
            element.getElementsByTagName("BingCampaignId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("BingCampaignId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.bingCampaignId = Long.valueOf(((Node) element.getElementsByTagName("BingCampaignId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing Bing Campaign id");
        }
        NodeList keywordDestinationUrlsAdGroupRequestsNode = element.getElementsByTagName("KeywordDestinationUrlsAdGroupRequestCollection");
        if (keywordDestinationUrlsAdGroupRequestsNode.getLength() > 0) {
            NodeList keywordDestinationUrlsAdGroupRequestNode = ((Element)keywordDestinationUrlsAdGroupRequestsNode.item(0)).getElementsByTagName("KeywordDestinationUrlsAdGroupRequest");
            if (keywordDestinationUrlsAdGroupRequestNode.getLength() <= 0) {
                throw new GxItemException("No AdGroups were found for Campaign " + this.gxCampaignId);
            }
            this.keywordDestinationUrlsAdGroupRequests = new GxKeywordDestinationUrlsAdGroupRequest[keywordDestinationUrlsAdGroupRequestNode.getLength()];
            for (int i=0; i<keywordDestinationUrlsAdGroupRequestNode.getLength(); i++) {
                keywordDestinationUrlsAdGroupRequests[i] = new GxKeywordDestinationUrlsAdGroupRequest((Element)keywordDestinationUrlsAdGroupRequestNode.item(i));
            }
        } else {
            this.keywordDestinationUrlsAdGroupRequests = new GxKeywordDestinationUrlsAdGroupRequest[0];
        }
    }

    public long getGxCampaignId() {
        return gxCampaignId;
    }

    public long getBingCampaignId() {
        return bingCampaignId;
    }

    public GxKeywordDestinationUrlsAdGroupRequest[] getKeywordDestinationUrlsAdGroupRequests() {
        return keywordDestinationUrlsAdGroupRequests;
    }
}
